<template>
	<base-page>
		<view slot="header">
			<!-- #ifdef MP-WEIXIN -->
			<cu-custom :isBack="true" bgColor="bg-white">
				<block slot="content">密码设置</block>
			</cu-custom>
			<!-- #endif -->
			<!-- #ifdef H5 -->
			<block>
				<cu-custom :isBack="true" bgColor="bg-white">
					<block slot="content">密码设置</block>
				</cu-custom>
			</block>
			<!-- #endif -->
		</view>
		<view slot="center" class="auto-100 content-wrapper bg-gray">
			<!-- 设置过密码 -->
			<view class="wrapper bg-white" v-if="userInfo.password">
				<view class="psw df-cen-rl boder-padding">
					<view class="item">旧密码</view>
					<input placeholder="请输入密码~" v-model="oldPsw" type="password" class="ipt" />
				</view>
				<view class="psw df-cen-rl boder-padding">
					<view class="item">新密码</view>
					<input placeholder="请输入密码~" v-model="newPsw" type="password" class="ipt" />
				</view>

				<view class="psw df-cen-rl boder-padding">
					<view class="item">确认密码</view>
					<input placeholder="请再次输入密码" v-model="confirmPsw" type="password" class="ipt" />
				</view>

				<view class="btn bg-gradual-orange round" @click="submitVote">确定</view>
			</view>
			<view class="wrapper bg-white" v-else>
				<!-- 未设置密码,第一次设置 -->
				<view class="psw df-cen-rl boder-padding">
					<view class="item">新密码</view>
					<input placeholder="请输入密码~" v-model="psw" type="password" class="ipt" />
				</view>
				<view class="psw df-cen-rl boder-padding">
					<view class="item">确认密码</view>
					<input placeholder="请再次输入密码" v-model="confirmPsw" type="password" class="ipt" />
				</view>
				<view class="psw df-cen-rl boder-padding">
					<input placeholder="请输入验证码" type="number" v-model="code" :maxlength="6" />
					<button class="cu-btn codeBtn" style="padding: 0;" @click="getCode" v-if="codeType">获取验证码</button>
					<view class="tiem-text " v-else>{{ tiemNum }}秒</view>
				</view>
				<view class="btn bg-gradual-orange round" @click="submitVote">确定</view>
			</view>
		</view>
	</base-page>
</template>

<script>
	import {
		setPswMobileCode,
		setPsw,
		getMemberDetail,
		changePsw
	} from '@/common/js/request/modules/public.js';

	export default {
		data() {
			return {
				isSaving: true,
				confirmPsw: '',
				psw: '',
				code: '',
				tiemNum: '',
				userInfo: {},
				codeType: true,
				loginType: true,
				tiemNum: 60,
				tiemConten: null,
				key: '',
				oldPsw: '',
				newPsw: '',
				configreg: {}
			};
		},
		onLoad(e) {},
		onShow(e) {
			this.$Apis.registerConfig().then(res => {
				this.configreg = res.value
				console.log("this.configreg", this.configreg)
			})
		},
		watch: {

		},
		computed: {},
		methods: {
			getCode() {
				console.log(this.userInfo.mobile);
				setPswMobileCode({
					mobile: this.userInfo.mobile
				}).then(res => {
					console.log("res", res)
					// this.$Utils.tip(res);
					this.codeType = false;
					this.key = res.key;
					this.tiemConten = setInterval(() => {
						this.tiemNum--;
						if (this.tiemNum === 0) {
							clearInterval(this.tiemConten);
							this.tiemNum = 60;
							this.codeType = true;
						}
					}, 1000);
				});
			},
			// 校验是否为数字
			isNumber(val) {
				var ptr_digit = /^.*[0-9]+.*$/;
				if (ptr_digit.test(val)) {
					return true;
				} else {
					return false;
				}
			},
			// 校验是否包含小写字母
			ptr_lowcaseCheck(val) {
				var ptr_lowcase = /^.*[a-z]+.*$/;
				if (ptr_lowcase.test(val)) {
					return true;
				} else {
					return false;
				}
			},
			// 校验是否包含大写字母
			ptr_upcaseCheck(val) {
				var ptr_upcase = /^.*[A-Z]+.*$/;
				if (ptr_upcase.test(val)) {
					return true;
				} else {
					return false;
				}
			},
			// 校验是否包含特殊字符（非数字、字母的字符）
			ptr_specialCheck(val) {
				var ptr_special = /((?=[\x21-\x7e]+)[^A-Za-z0-9])/;
				if (ptr_special.test(val)) {
					return true;
				} else {
					return false;
				}
			},
			// 校验全部
			validatePass(value) {
				//是否包含数字
				var ptr_digit = /^.*[0-9]+.*$/;
				//是否包含小写字母
				var ptr_lowcase = /^.*[a-z]+.*$/;
				//是否包含大写字母
				var ptr_upcase = /^.*[A-Z]+.*$/;
				//是否包含特殊字符（非数字、字母的字符）
				var ptr_special = /((?=[\x21-\x7e]+)[^A-Za-z0-9])/;
				if ((ptr_digit.test(value) && ptr_lowcase.test(value) && ptr_upcase.test(value) && ptr_special.test(
						value)) ||
					(!ptr_digit.test(value) && ptr_lowcase.test(value) && ptr_upcase.test(value) && ptr_special.test(
						value)) ||
					(ptr_digit.test(value) && !ptr_lowcase.test(value) && ptr_upcase.test(value) && ptr_special.test(
						value)) ||
					(ptr_digit.test(value) && ptr_lowcase.test(value) && !ptr_upcase.test(value) && ptr_special.test(
						value)) ||
					(ptr_digit.test(value) && ptr_lowcase.test(value) && ptr_upcase.test(value) && !ptr_special.test(
						value))) {
					return true;
				} else {
					return false;
				}
			},
			isCheck() {

				let arr = this.configreg.pwd_complexity.split(',')

				let pwdCount = Number(this.configreg.pwd_len)

				if (this.newPsw.length < pwdCount) {
					return {
						isPass: false,
						msg: `新密码长度少于${pwdCount}位`
					};
				}

				if (this.confirmPsw.length < pwdCount) {
					return {
						isPass: false,
						msg: `确认密码长度少于${pwdCount}位`
					};
				}

				if (arr.length == 4) {
					console.log('校验全部')
					if (this.newPsw) {
						let newpswnb = this.validatePass(this.newPsw)
						if (newpswnb == false) {
							return {
								isPass: false,
								msg: `至少包含大写字母、小写字母、数字、特殊字符中的三类字符`
							};
						}
					}
					if (this.confirmPsw) {
						let confirmPswnb = this.validatePass(this.confirmPsw)
						if (confirmPswnb == false) {
							return {
								isPass: false,
								msg: `至少包含大写字母、小写字母、数字、特殊字符中的三类字符`
							};
						}
					}
					if (this.psw) {
						let pswnb = this.validatePass(this.psw)
						if (pswnb == false) {
							return {
								isPass: false,
								msg: `至少包含大写字母、小写字母、数字、特殊字符中的三类字符`
							};
						}
					}
				} else {
					console.log("校验选中的")
					arr.map((item, index) => {
						switch (item) {
							case 'number':
								console.log("校验Number")
								if (this.newPsw) {
									let newpswnb = this.isNumber(this.newPsw)
									if (newpswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含数字`
										};
									}
								}
								if (this.confirmPsw) {
									let confirmPswnb = this.isNumber(this.confirmPsw)
									if (confirmPswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含数字`
										};
									}
								}
								if (this.psw) {
									let pswnb = this.isNumber(this.psw)
									if (pswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含数字`
										};
									}
								}
								break;
							case 'letter':
								console.log("校验小写字母")
								if (this.newPsw) {
									let newpswnb = this.ptr_lowcaseCheck(this.newPsw)
									if (newpswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含小写字母`
										};
									}
								}
								if (this.confirmPsw) {
									let confirmPswnb = this.ptr_lowcaseCheck(this.confirmPsw)
									if (confirmPswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含小写字母`
										};
									}
								}
								if (this.psw) {
									let pswnb = this.ptr_lowcaseCheck(this.psw)
									if (pswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含小写字母`
										};
									}
								}
								break;
							case 'upper_case':
								console.log("校验大写字母")
								if (this.newPsw) {
									let newpswnb = this.ptr_upcaseCheck(this.newPsw)
									if (newpswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含大写字母`
										};
									}
								}
								if (this.confirmPsw) {
									let confirmPswnb = this.ptr_upcaseCheck(this.confirmPsw)
									if (confirmPswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含大写字母`
										};
									}
								}
								if (this.psw) {
									let pswnb = this.ptr_upcaseCheck(this.psw)
									if (pswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含大写字母`
										};
									}
								}
								break;
							case 'symbol':
								console.log("校验标点符号")
								if (this.newPsw) {
									let newpswnb = this.ptr_specialCheck(this.newPsw)
									if (newpswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含大写字母`
										};
									}
								}
								if (this.confirmPsw) {
									let confirmPswnb = this.ptr_specialCheck(this.confirmPsw)
									if (confirmPswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含大写字母`
										};
									}
								}
								if (this.psw) {
									let pswnb = this.ptr_specialCheck(this.psw)
									if (pswnb == false) {
										return {
											isPass: false,
											msg: `密码必须包含大写字母`
										};
									}
								}
								break;
							default:
								console.log("没有")
						}
					})
				}

				if (!this.userInfo.password) {
					if (!`${this.psw}`.length) {
						return {
							isPass: false,
							msg: '请输入密码'
						};
					}
					if (!`${this.confirmPsw}`.length) {
						return {
							isPass: false,
							msg: '请输入确认密码'
						};
					}
					if (this.psw !== this.confirmPsw) {
						return {
							isPass: false,
							msg: '两次密码输入不一致'
						};
					}
					if (!/[0-9]{4}/.test(this.code) || `${this.code}`.length !== 4) {
						return {
							msg: '请输入验证码',
							isPass: false
						};
					}
					return {
						isPass: true,
						msg: '通过'
					};
				} else {
					if (!`${this.oldPsw}`.length) {
						return {
							isPass: false,
							msg: '请输入旧密码'
						};
					}
					if (!`${this.newPsw}`.length) {
						return {
							isPass: false,
							msg: '请输入新密码'
						};
					}
					if (!`${this.confirmPsw}`.length) {
						return {
							isPass: false,
							msg: '请输入确认密码'
						};
					}
					if (this.newPsw !== this.confirmPsw) {
						return {
							isPass: false,
							msg: '两次密码输入不一致'
						};
					}
					return {
						isPass: true,
						msg: '通过'
					};
				}
			},
			submitVote() {
				const {
					isPass,
					msg
				} = this.isCheck();
				if (!isPass) {
					this.$Utils.tip(msg);
					return false;
				}
				// this.goSetPsw();
			},
			goSetPsw() {
				if (!this.userInfo.password) {
					setPsw({
							mobile: this.userInfo.mobile,
							code: this.code,
							password: this.psw,
							key: this.key
						})
						.then(res => {
							console.log(res);
							this.$Utils.tip('设置成功');
							this.exitPage();
						})
						.catch(err => {
							console.log(err);
						});
				} else {
					changePsw({
							old_password: this.oldPsw,
							new_password: this.newPsw
						})
						.then(res => {
							console.log(res);
							this.$Utils.tip('更改成功');
							this.exitPage();
						})
						.catch(err => {
							console.log(err);
						});
				}
			},
			getUserInfo() {
				getMemberDetail().then(res => {
					this.userInfo = res;
					// console.log(this.userInfo);
				});
			},
			exitPage() {
				if (this.isNativeApp()) {
					sdk.exit();
				} else {
					uni.navigateBack({
						delta: 1
					});
				}
			}
		},

		mounted() {
			this.getUserInfo();
		}
	};
</script>

<style lang="scss">
	.codeBtn {
		background-color: #fff;
		color: rgb(102, 102, 102);
	}

	.wrapper {
		padding: 0 15px 30px;

		.psw {
			.ipt {
				flex: 0.5;
				text-align: right;
			}

			.item {
				flex: 0.5;
			}
		}

		.btn {
			margin: 30px auto 0;
			width: 80%;
			padding: 10px 0;
			text-align: center;
			font-size: 16px;
		}
	}

	.boder-padding {
		background-color: #fff;
		padding: 15px 0;
		border-bottom: 1px solid #f4f4f4;
	}
</style>
